﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
using DataAccessLayer.Security;
using Common;

namespace DataAccessLayer.DAL
{
    public class SellerDAL
    {
        public static List<ProductCategory> GetSellProduct(String username)
        {
            List<ProductCategory> list = new List<ProductCategory>();
            using (var context = new ReverseAuctionEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = false;
                var result = context.Users.SingleOrDefault(u => u.Username == username);
                result.ProductCategories.Load();
                list = result.ProductCategories.ToList();
            }
            
            return list;
        }

        public static void AddSellProduct(int userID, List<ProductCategory> listproduct)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Users.FirstOrDefault(u => u.ID == userID);
                result.ProductCategories.Load();
                if (result != null)
                {
                    result.ProductCategories.Clear();
                    foreach (var p in listproduct)
                    {
                        var product = context.ProductCategories.FirstOrDefault(pi => pi.ID == p.ID);
                        if (product != null)
                        {
                            context.ProductCategories.ApplyCurrentValues(product);
                            result.ProductCategories.Add(product);
                        }
                    }
                    context.Users.ApplyCurrentValues(result);
                    context.SaveChanges();
                }
            }
        }

        public static void UpdateInfoSentBuyer(int userID, String info)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var usersetting = context.UserSettings.SingleOrDefault(u => u.UserID == userID);
                if (usersetting != null)
                {
                    usersetting.ContactInfo = info;
                    context.SaveChanges();
                }
                else
                {
                    UserSetting user = new UserSetting();
                    user.UserID = userID;
                    user.ContactInfo = info;
                    context.UserSettings.AddObject(user);
                    context.SaveChanges();
                }
            }
        }

        public static UserSetting GetUserSetting(int userID)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.UserSettings.SingleOrDefault(u => u.UserID == userID);
                return result;
            }
        }

        /// <summary>
        /// Get all join auction
        /// </summary>
        /// <param name="SellerBid">SellerBid</param>
        /// <returns></returns>
        public static List<Auction> GetAllJoinAuction(int userID)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Users.Include("SellerBids").Include("SellerBids.Auction").SingleOrDefault(u => u.ID == userID);
                var distinct = result.SellerBids.Select(u => u.Auction).Distinct().ToList();
                //var t = result.SellerBids.OrderBy(u => u.Auction.EndTime).ToList();
                return distinct;
            }
        }

        /// <summary>
        /// Get all join auction
        /// </summary>
        public static User GetWonAuctions(int userID)
        {
            using (var context = new ReverseAuctionEntities())
            {
                return context.Users.Include("AuctionTransactions").Include("AuctionTransactions.Auction").SingleOrDefault(u => u.ID == userID);
            }
        }

        /// <summary>
        /// Add AdvertiseProduct
        /// </summary>

        public static User GetAdvertiseProduct(int userID)
        {
            using (var context = new ReverseAuctionEntities())
            {
                return context.Users.Include("AdvertiseProducts").Include("AdvertiseProducts.ProductCategory")
                    .SingleOrDefault(u => u.ID == userID);
            }
        }

        public static void AddAdvertiseProduct(AdvertiseProduct adv, List<string> listImg)
        {
            using (var context = new ReverseAuctionEntities())
            {
                context.AdvertiseProducts.AddObject(adv);
                context.SaveChanges();
                foreach (string str in listImg)
                {
                    ImageData img = new ImageData();
                    img.ParentTable = "AdvertiseProduct";
                    img.ParentID = adv.ID;
                    img.ImageUrl = str;
                    img.Data = new byte[1];
                    context.ImageDatas.AddObject(img);
                }
                context.SaveChanges();
            }
        }
    

    }
}
